package cloud.pangui.core.utils;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @Auther hepangui
 * @Date 2020/11/5 0005
 */
public class ArrayUtils {
	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static int[] append(int[] array, int... item) {
		int[] newArray = new int[array.length + item.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static double[] append(double[] array, double... item) {
		double[] newArray = new double[array.length + item.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static byte[] append(byte[] array, byte... item) {
		byte[] newArray = new byte[array.length + item.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static boolean[] append(boolean[] array, boolean... item) {
		boolean[] newArray = new boolean[array.length + item.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static char[] append(char[] array, char... item) {
		char[] newArray = new char[array.length + item.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static long[] append(long[] array, long... item) {
		long[] newArray = new long[array.length + item.length];
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 拼接
	 *
	 * @param array 原数组
	 * @param item  拼接项，可传多个
	 * @return
	 */
	public static <T> T[] append(T[] array, T... item) {
		Class aClass = item[0].getClass();
		T[] newArray = (T[]) Array.newInstance(aClass, array.length + item.length);
		System.arraycopy(array, 0, newArray, 0, array.length);
		System.arraycopy(item, 0, newArray, array.length, item.length);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static int[] sub(int[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		int[] newArray = new int[end - start];
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static long[] sub(long[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		long[] newArray = new long[end - start];
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static double[] sub(double[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		double[] newArray = new double[end - start];
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static byte[] sub(byte[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		byte[] newArray = new byte[end - start];
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static boolean[] sub(boolean[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		boolean[] newArray = new boolean[end - start];
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static char[] sub(char[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		char[] newArray = new char[end - start];
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 截取数组，含前不含后
	 *
	 * @param array 要截取的数组
	 * @param start 开始位
	 * @param end   结束位
	 * @return
	 */
	public static <T> T[] sub(T[] array, int start, int end) {
		if (start > array.length || end > array.length) {
			throw new IndexOutOfBoundsException();
		}
		if (end < start) {
			throw new IllegalArgumentException(end + "");
		}
		if (array.length == 0) {
			throw new IllegalArgumentException("length");
		}
		Class aClass = array[0].getClass();
		T[] newArray = (T[]) Array.newInstance(aClass, end - start);
		System.arraycopy(array, start, newArray, 0, end - start);
		return newArray;
	}

	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static int indexOf(int [] array, int value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static int indexOf(long [] array, long value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static int indexOf(double [] array, double value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static int indexOf(byte [] array, byte value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static int indexOf(boolean [] array, boolean value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static int indexOf(char [] array, char value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 获取下标位置
	 *
	 * @param array
	 * @param value
	 * @return
	 */
	public static <T> int indexOf(T [] array, T value) {
		for (int i = 0; i < array.length; i++) {
			if (value == array[i]) {
				return i;
			}
		}
		return -1;
	}

	public static void main(String[] args) {

		String aaa = "\t/**\n" +
				"\t * 获取下标位置\n" +
				"\t *\n" +
				"\t * @param array\n" +
				"\t * @param value\n" +
				"\t * @return\n" +
				"\t */\n" +
				"\tpublic static long indexOf(long [] array, long value) {\n" +
				"\t\tfor (int i = 0; i < array.length; i++) {\n" +
				"\t\t\tif (value == array[i]) {\n" +
				"\t\t\t\treturn i;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t\treturn -1;\n" +
				"\t}";
		System.out.println(aaa.replace("long", "int"));
		System.out.println(aaa);
		System.out.println(aaa.replace("long", "double"));
		System.out.println(aaa.replace("long", "byte"));
		System.out.println(aaa.replace("long", "boolean"));
		System.out.println(aaa.replace("long", "char"));

		String[] a = new String[]{"a"};
		String[] b = new String[]{"b"};
		System.out.println(Arrays.toString(ArrayUtils.append(a, b)));

		int[] ints = {1, 2, 3};
		int[] append = ArrayUtils.sub(ints, 1, 3);
		System.out.println(Arrays.toString(append));
	}
}
